package com.windystory.app.antdesign;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.windystory.wslibrary.base.activity.BaseActivity;
import com.windystory.wslibrary.util.LogUtil;
import com.windystory.wsmedialibrary.camera.CameraManager;
import com.windystory.wsmedialibrary.camera.CameraPreview;
import com.windystory.wsmedialibrary.util.BitmapUtils;

public class UDPActivity extends BaseActivity {

    public static final int CAMERA_FACING_BACK = 0;
    public static final int CAMERA_FACING_FRONT = 1;

    private CameraPreview mPreview;
    private Button switcher,flashlight;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_udp;
    }

    @Override
    protected boolean setEnableSwipeBack() {
        return false;
    }

    @Override
    protected void initView() {
        flashlight = (Button)findViewById(R.id.flashlight);
        switcher = (Button) findViewById(R.id.swticher);
    }

    @Override
    protected void initData() {
        if (!checkCameraHardware(this)) {
            Toast.makeText(this, "没有检测到摄像机设备", Toast.LENGTH_SHORT).show();
            return;
        }
        FrameLayout preview = (FrameLayout) findViewById(R.id.cameraView);
        mPreview = new CameraPreview(this, preview);
    }

    @Override
    protected void initListener() {
        switcher.setOnClickListener(view -> {
            if (i % 2 == 0) {
                mPreview.changeCamera();
                LogUtil.e("CAMERA_FACING_BACK");
            } else {
                mPreview.changeCamera();
                LogUtil.e("CAMERA_FACING_FRONT");
            }
            i++;
        });

        flashlight.setOnClickListener(view -> {
            if (ii % 2 == 0) {
                mPreview.turnLightOn();
                LogUtil.e("turnLightOn");
            } else {
                mPreview.turnLightOff();
                LogUtil.e("turnLightOff");
            }
            ii++;
        });
    }

    static int i = 0;
    static int ii = 0;
    public void takePic(View v) {
        mPreview.takePicture(new CameraManager.OnCameraActionCallback() {
            @Override
            public void onTakePictureComplete(Bitmap bitmap) {
                BitmapUtils.saveFile(Environment.getExternalStorageDirectory() + "/1.png", bitmap);
                mPreview.onResume();
            }
        });
    }




    /**
     * 检查有无摄像机
     *
     * @param context
     * @return
     */
    private boolean checkCameraHardware(Context context) {
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPreview.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPreview.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPreview.onDestroy();
    }
}
